package com.imau.controller;

import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.imau.bean.Department;
import com.imau.common.entiry.PageQuery;
import com.imau.common.entiry.ResultCode;
import com.imau.common.entiry.ResultEntity;
import com.imau.common.utils.JSONUtil;
import com.imau.service.DepartmentService;
import com.imau.service.imp.DepartmentServiceImp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "DepartmentConterllor", value = "/dept/*")
public class DepartmentConterllor extends Baseconterllor {

    public DepartmentConterllor() {

        this.service = new DepartmentServiceImp();
    }

    /**添加教师*/
    public void add(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {

        //1.获取前端
        BufferedReader reader = request.getReader();

        String jsonObj = reader.readLine();

        //2.转换
        Department department = JSONUtil.jsonToEntity(jsonObj, Department.class);

        //3.调用服务

        boolean b = service.saveEntity(department);

        //4.响应数据

        this.responseBody(response,b,new ResultEntity(ResultCode.SUCCESS));
    }

    public void delete(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
    {


        //1.获取前台主键id
        String id = request.getParameter("id");

        //2.业务处理

        boolean b = service.removeWithById(id);

        //3.响应数据


        this.responseBody(response,b,new ResultEntity(ResultCode.SUCCESS));

    }


    public void update (HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
    {

        //1.获取前台的JSON串
        BufferedReader reader = request.getReader();

        String jsonStr = reader.readLine();

        Department department = JSONUtil.jsonToEntity(jsonStr, Department.class);
        //2.调用service方法添加

        boolean b = service.updateByEntity(department);

        //3.响应数据

        this.responseBody(response,b,new ResultEntity(ResultCode.SUCCESS));


    }


    public void deptTree(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{

        //2.获取前台的JSON串
        BufferedReader reader = request.getReader();

        String jsonStr = reader.readLine();
        //3.转成对象

        Department department = JSONUtil.jsonToEntity(jsonStr, Department.class);

        List<Tree<String>> trees = ((DepartmentService) service).selectDeptTreeList(department);

        this.responseBody(response,trees==null?false:true,new ResultEntity(ResultCode.SUCCESS,trees));



    }

    public void getByPage(HttpServletRequest request, HttpServletResponse response) throws Exception {


        //1.获取前台分页数据
        Integer pageNum = Integer.valueOf(request.getParameter("pageNum"));
        Integer pageSize =  Integer.valueOf(request.getParameter("pageSize"));

        //2.获取前台的JSON串
        BufferedReader reader = request.getReader();

        String jsonStr = reader.readLine();


        //3.转成对象

        Department department = JSONUtil.jsonToEntity(jsonStr, Department.class);


        //4.调用service方法添加

        Page<Department> page = service.queryPageList(department,new PageQuery(pageNum,pageSize));

        //3.响应数据

        this.responseBody(response,page==null?false:true,new ResultEntity(ResultCode.SUCCESS,page));


    }


    public void getInfo(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
    {


        //1.获取前台主键id
        String id = request.getParameter("id");

        //2.业务处理

        Department department = ((DepartmentService) service).queryById(id);

        //3.响应数据


        this.responseBody(response,department!=null?true:false,new ResultEntity(ResultCode.SUCCESS,department));

    }
}
